//计算成本
function calculatedCosts(data) {

    if (isNotEmpty(data[0].JSON_CASECOST_LIST)) {


        var oldCostList;
        //获取数据库中的历史生效成本
        if (typeof data[0].JSON_CASECOST_LIST == 'string') {
            oldCostList = JSON.parse(data[0].JSON_CASECOST_LIST);
        } else {
            oldCostList = data[0].JSON_CASECOST_LIST;
        }
        //先获取itemNBR
        var itemNbr = data[0].ITEM_NBR;
        var size = data[0].VNPK_QTY;
        var rate = data[0].TAX_RATE;
        //先找出当前form中存在的相同itemNbr
        var itemList = [];
        var oldList = [];
        var now2 = new Date();
        //在数组中放置数据库中的有效日期,数据库成本
        if (isNotEmpty(oldCostList)) {
            for (var i2 = 0; i2 < oldCostList.length; i2++) {
                var oldEffDate = oldCostList[i2].EFFECTIVE_DATE;
                var oldExpDate = oldCostList[i2].EXPIRATION_DATE;
                var oldCost = oldCostList[i2].VNPK_PPD_COST_AMT;
                if (isNotEmpty(oldExpDate) && oldExpDate === '2049-12-31') {
                    var oldItemNbrList = {
                        itemNbrT: itemNbr,
                        effDate: oldEffDate,
                        expDate: oldExpDate,
                        cost: oldCost,
                        size: size,
                        rate: rate,
                        type: 'old',
                        change: false
                    };
                    itemList.push(oldItemNbrList);
                    oldList.push(oldItemNbrList);
                }

            }
        }
        if (isNotEmpty(data)) {
            //组装修改的成本数据进入成本判断数组中，修改成本数据
            for (var i = 0; i < data.length; i++) {
                //排序后的第一个itemNbr
                if (isNotEmpty(data[i].ITEM_NBR) && data[i].ITEM_NBR === itemNbr && isNotEmpty(data[i].NEW_CASE_COST)) {
                    var effDate = data[i].NEW_COST_EFF_DATE;
                    var expDate = data[i].NEW_COST_EXP_DATE;
                    var cost = data[i].NEW_CASE_COST;
                    if (expDate !== '2049-12-31'){
                        var itemNbrList = {
                            id: data[i].ID,
                            itemNbrT: itemNbr,
                            effDate: effDate,
                            expDate: '2049-12-31',
                            cost: cost,
                            size: size,
                            rate: rate,
                            type: 'new',
                            change: false
                        };
                        var itemNbrAfe = {
                            id: data[i].ID,
                            itemNbrT: itemNbr,
                            effDate: expDate,
                            expDate: '2049-12-31',
                            cost: cost,
                            size: size,
                            rate: rate,
                            type: 'new',
                            change: false
                        };
                        for (var od = 0; od < oldList.length; od++) {
                            if (dateCompare(oldList[od].effDate,itemNbrAfe.effDate)>=0){
                                itemNbrAfe.cost = oldList[od].cost;
                            }
                        }
                        itemList.push(itemNbrList);
                        itemList.push(itemNbrAfe);
                    }else {
                        var itemNbrNew = {
                            id: data[i].ID,
                            itemNbrT: itemNbr,
                            effDate: effDate,
                            expDate: expDate,
                            cost: cost,
                            size: size,
                            rate: rate,
                            type: 'new',
                            change: false
                        };
                        itemList.push(itemNbrNew);
                    }
                }
            }
        }
        //将组装好的数组数据进行排序，按照开始时间进行排序
        itemList.sort(function (a, b) {
            return a.effDate > b.effDate ? 1 : -1;
        });
        var oldListDeep = deepCopy(oldList);
        if (isNotEmpty(itemList)) {
            for (var s = 0; s < itemList.length; s++) {
                //获取单个对象
                var item = itemList[s];
                var y = s + 1;
                var x = s - 1;
                var itemBef;
                var itemAfter;
                if (x >= 0) {
                    //获取前一时间段
                    itemBef = itemList[x]
                }
                var changeLast = false;
                if (y === itemList.length) {
                    itemAfter = item;
                    changeLast = true;
                } else {
                    //获取下一个时间段
                    itemAfter = itemList[y];
                }
                //如果新Cost的结束时间为2049,并且不为最后一条时,删除后面所有的旧CSRL
                if (isNotEmpty(item.type) && item.type === 'new' && item.expDate === '2049-12-31' && !changeLast && isNotEmpty(itemAfter) && itemAfter.type === 'old') {
                    itemList.splice(y, 1);
                    s--;
                    continue;
                }
                if ((isNotEmpty(item.type) && item.type === 'old') || item.expDate === '2049-12-31') {
                    //更改旧值结束日期为新值开始日期
                    if (!changeLast) {
                        item.expDate = itemAfter.effDate;
                        item.change = true;
                    }
                }
                //如果不是第一条数据
                if (isNotEmpty(itemBef)) {
                    //如果时第一位则比较后面的时间字段
                    var flag1 = isDateBetween(item.effDate, itemBef.effDate, itemBef.expDate);
                    //如果当前时间的开始日期再前一个时间的范围值内，或者两个开始时间相等，则删除当前数据，循环索引减1
                    if (flag1 && item.expDate === itemAfter.effDate && Date.parse(itemBef.expDate) < Date.parse(itemAfter.effDate)) {

                        item.effDate = itemBef.expDate;
                        item.change = true;

                    } else if (flag1 || item.effDate === itemBef.effDate) {
                        var filter2 = oldListDeep.filter(function (value) {
                            return  isDateIntersection_Min(item.effDate,item.expDate,value.effDate,value.expDate)
                        });
                        var beforeCost2;
                        if (isNotEmpty(filter2)){
                            beforeCost2 = filter2[filter2.length-1].cost;
                        }else {
                            beforeCost2 = itemBef.cost;
                        }
                        itemList.splice(s, 1);
                        s = s - 1;
                        if (changeLast) {
                            var addItemLast = {
                                id: itemBef.id,
                                itemNbrT: item.itemNbrT,
                                effDate: itemBef.expDate,
                                expDate: '2049-12-31',
                                cost: beforeCost2,
                                size: size,
                                rate: rate,
                                type: 'new',
                                change: false
                            };
                            insert(itemList, itemList.length, addItemLast);
                            break;
                        }
                        continue;
                    }
                    if (Date.parse(item.expDate) < Date.parse(itemAfter.effDate) && item.type !== 'old') {
                        var filter = oldListDeep.filter(function (value) {
                            return  isDateIntersection_Min(item.effDate,item.expDate,value.effDate,value.expDate)
                        });
                        var beforeCost;
                        if (isNotEmpty(filter)){
                            beforeCost = filter[filter.length-1].cost;
                        }else {
                            beforeCost = itemBef.cost;
                        }
                        var addItem = {
                            id: item.id,
                            itemNbrT: item.itemNbrT,
                            effDate: item.expDate,
                            expDate: itemAfter.effDate,
                            cost: beforeCost,
                            size: size,
                            rate: rate,
                            type: 'new',
                            change: false
                        };
                        insert(itemList, y, addItem);
                        s--;
                    }
                }

            }
        }
        return itemList;
    }
}
